# 14. 找朋友
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.on('line', (line) => {
let n = parseInt(line);
rl.on('line', (line) => {
let height = line.split(' ').map(Number);
let friendIndex = [];
for(let i=0; i<height.length; i++) {
for(let j=i+1; j<height.length; j++) {
if(height[i] < height[j]) {
friendIndex.push(j);
break;
}
if (j===height.length - 1) {
friendIndex.push(0);
}
}
if (i===height.length - 1) {
friendIndex.push(0);
}
}
let res = "";
for(let i=0; i<n; i++) {
res += friendIndex[i] + ' ';
}
console.log(res);
// let stack = [0];
// for(let i=1; i<n; i++) {
// while(stack.length && height[i] > height[stack[stack.length - 1]]) {
// friendIndex[stack.pop()] = i;
// }
// stack.push(i)
// }
});
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
← 13. 转盘寿司 15. 爱吃蟠桃的孙悟空 →